package uk.co.hephistocles.blobGame;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class AnimatedImage extends Actor {
        public TextureRegion region = null;
        private int frameNum;
        private int totFrames;
        private int sheetWidth;
        private int invFPS;
        private long lastUpdate;
        private float invTexWidth;
        private float invTexHeight;
        

        public AnimatedImage (String name) {
                super(name);
        }

        public AnimatedImage (String name, Texture texture, int width, int height, int frames, int sheetWidth, int fps) {
                super(name);
                this.originX = width / 2.0f;
                this.originY = height / 2.0f;
                this.width = width;
                this.height = height;
                this.region = new TextureRegion(texture);
                
                this.totFrames = frames;
                this.sheetWidth = sheetWidth;
                this.invTexWidth = 1f / texture.getWidth();
                this.invTexHeight = 1f / texture.getHeight();
                this.invFPS = 1/fps;
        }

        public AnimatedImage (String name, TextureRegion region, int width, int height, int frames, int sheetWidth, int fps) {
                super(name);
                this.originX = width / 2.0f;
                this.originY = height / 2.0f;
                this.width = width;
                this.height = height;
                this.region = new TextureRegion(region);
                
                this.totFrames = frames;
                this.sheetWidth = sheetWidth;
                this.invTexWidth = 1f / region.getRegionWidth();
                this.invTexHeight = 1f / region.getRegionHeight();
                this.invFPS = 1/fps;
        }
        public void random() {
    		this.frameNum = (int)(Math.random()*totFrames);
        }
        
        public void update() {
    		//increase the animation's frame number
    		frameNum++;
    		//loop back to 0 at the end of animation
    		if (frameNum>=totFrames) {
    			frameNum = 0;
    		}
    		//region.setRegion((int)frameNum%sheetWidth*width,(int)height*(frameNum - frameNum%sheetWidth)/sheetWidth,(int)width,(int) height);
    		region.setRegion(frameNum%sheetWidth*width * invTexWidth, 
    				(height*(frameNum - frameNum%sheetWidth)/sheetWidth) * invTexHeight,
    				(frameNum%sheetWidth*width + width) * invTexWidth,
    				(height*(frameNum - frameNum%sheetWidth)/sheetWidth + height) * invTexHeight);
        }

        @Override
        public void draw (SpriteBatch batch, float parentAlpha) {
        		if (System.currentTimeMillis() - lastUpdate > invFPS) {
        			lastUpdate = System.currentTimeMillis();
        			update();
        		}
                if (region.getTexture() != null) {
                        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
                        if (scaleX == 1 && scaleY == 1 && rotation == 0)
                                batch.draw(region, x, y, width, height);
                        else
                                batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
                }
        }

        @Override
        public boolean touchDown (float x, float y, int pointer) {
                return x > 0 && y > 0 && x < width && y < height;
        }

        @Override
        public boolean touchUp (float x, float y, int pointer) {
                return false;
        }

        @Override
        public boolean touchDragged (float x, float y, int pointer) {
                return false;
        }

        public Actor hit (float x, float y) {
                if (x > 0 && x < width) if (y > 0 && y < height) return this;

                return null;
        }
}


/*package uk.co.hephistocles.blobGame;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class AnimatedImage extends Actor {
        public TextureRegion region = null;
        private int frameNum = 0;
        private int totFrames;
        private int sheetWidth;
        private Texture tex;

        public AnimatedImage (String name) {
                super(name);
        }

        public AnimatedImage (String name, Texture texture, int width, int height, int frames, int sheetWidth) {
                super(name);
                this.originX = width / 2.0f;
                this.originY = height / 2.0f;
                this.width = width;
                this.height = height;
                this.tex = texture;
                
                this.sheetWidth = sheetWidth;
                this.totFrames = frames;
        }

        public AnimatedImage (String name, TextureRegion region, int width, int height, int frames, int sheetWidth) {
                super(name);
                this.originX = width / 2.0f;
                this.originY = height / 2.0f;
                this.width = width;
                this.height = height;
                this.tex = region.getTexture();
                
                this.sheetWidth = sheetWidth;
                this.totFrames = frames;
        }
        
        public void update(int dt) {
    		//increase the animation's frame number
    		frameNum++;
    		//loop back to 0 at the end of animation
    		if (frameNum>=totFrames) {
    			frameNum = 0;
    		}        	
        }

        @Override
        public void draw (SpriteBatch batch, float parentAlpha) {
                if (region.getTexture() != null) {
                        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
                        if (scaleX == 1 && scaleY == 1 && rotation == 0)
                                batch.draw(region, x, y, width, height);
                        else
                                batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation, 
                                		, false, false);
                }
        }

        @Override
        public boolean touchDown (float x, float y, int pointer) {
                return x > 0 && y > 0 && x < width && y < height;
        }

        @Override
        public boolean touchUp (float x, float y, int pointer) {
                return false;
        }

        @Override
        public boolean touchDragged (float x, float y, int pointer) {
                return false;
        }

        public Actor hit (float x, float y) {
                if (x > 0 && x < width) if (y > 0 && y < height) return this;

                return null;
        }
}*/